package com.example.demo_dongruanD.repository;

import com.example.demo_dongruanD.pojo.Orders;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrdersRepository extends JpaRepository<Orders, Integer> {

    List<Orders> findByDocId(Integer docId);

    @Query("SELECT o FROM Orders o WHERE (:userId IS NULL OR o.userId = :userId) AND " +
            "(:realName IS NULL OR o.userId IN (SELECT u.userId FROM Users u WHERE u.realName = :realName)) AND " +
            "(:sex IS NULL OR o.userId IN (SELECT u.userId FROM Users u WHERE u.sex = :sex)) AND " +
            "(:smId IS NULL OR o.smId = :smId) AND " +
            "(:orderDate IS NULL OR o.orderDate = :orderDate) AND " +
            "(:state IS NULL OR o.state = :state) AND " +
            "(:docId IS NULL OR o.docId = :docId)")
    List<Orders> findAllByConditions(@Param("userId") String userId, @Param("realName") String realName,
                                     @Param("sex") Integer sex, @Param("smId") Integer smId,
                                     @Param("orderDate") String orderDate, @Param("state") Integer state,
                                     @Param("docId") Integer docId);
}
